package com.cn.ZhiKeDianShang.util.PaoMaDeng;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.LinearSnapHelper;
import androidx.recyclerview.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;

import com.cn.ZhiKeDianShang.util.LogUtil;

import java.lang.ref.WeakReference;

import static androidx.recyclerview.widget.RecyclerView.SCROLL_STATE_IDLE;

public class MyBannerManger extends LinearLayoutManager {

    private RecyclerView mRecyclerView;
    private InterValHander mHander;
    private static final int INTERVAL = 3000;
    private boolean mIsLayoutComplete = false;
    private static int position = 1;
    private LinearSnapHelper mSnapHelper;

    public MyBannerManger(Context context, RecyclerView recyclerView) {
        super(context);
        setOrientation(HORIZONTAL);
        mRecyclerView = recyclerView;
        mHander = new InterValHander(this);
        mRecyclerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    mHander.removeCallbacksAndMessages(null);
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    mHander.sendEmptyMessageDelayed(position, INTERVAL);
                }
                return false;
            }
        });
    }

    @Override
    public void onAttachedToWindow(RecyclerView view) {
        super.onAttachedToWindow(view);
//        Log.d("Debug", "onAttachedToWindow");
        mSnapHelper = new LinearSnapHelper();
        mSnapHelper.attachToRecyclerView(mRecyclerView);
    }

    public void removeHanler() {
        mHander.removeCallbacksAndMessages(null);
        position = 1;
    }

    public void sendEmptyMessage() {
        mHander.sendEmptyMessageDelayed(1, INTERVAL);
    }

    @Override
    public void onLayoutCompleted(RecyclerView.State state) {
        super.onLayoutCompleted(state);
//        Log.d("Debug", "到达onLayoutCompleted中");
        if (!mIsLayoutComplete) {
            mHander.sendEmptyMessageDelayed(position, INTERVAL);
        }
        mIsLayoutComplete = true;
    }

    private RecyclerView getRecyclerView() {
        return mRecyclerView;
    }

    static class InterValHander extends Handler {

        private WeakReference<MyBannerManger> mWeakReference;
        private MyBannerManger mBannerManger;

        public InterValHander(MyBannerManger myBannerManger) {
            mWeakReference = new WeakReference<>(myBannerManger);
            mBannerManger = myBannerManger;
        }

        @Override
        public void handleMessage(Message msg) {
//            Log.d("Debug", "到达接收到消息这里" + msg + position);
            mBannerManger.getRecyclerView().smoothScrollToPosition(position);
            sendEmptyMessageDelayed(0, INTERVAL);
            position++;
        }
    }


    @Override
    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
        LogUtil.d("smoothScrollToPosition");
//        Log.d("Debug", "到达smoothScrollToPosition" + position);
        LinearSmoothScroller smoothScroller =
                new LinearSmoothScroller(recyclerView.getContext()) {
                    // 返回：滑过1px时经历的时间(ms)。
                    @Override
                    protected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {
                        float f = 150f / displayMetrics.densityDpi;
                        LogUtil.d("F = " + f);
                        return 0.5f;
                    }
                };
        smoothScroller.setTargetPosition(position);
        startSmoothScroll(smoothScroller);
    }

    @Override
    public void onScrollStateChanged(int state) {
//        Log.d("Debug", "滑动经过这里onScrollStateChanged");
        if (state == SCROLL_STATE_IDLE) {
            position = getPosition(mSnapHelper.findSnapView(this)) + 1;
        }

    }
}

